//
//  HJMainViewController.swift
//  zhangmiSinweibo
//
//  Created by zhangmi on 16/5/19.
//  Copyright © 2016年 Paramount Pictures. All rights reserved.
//

//cmd + shfit + J
//获取文件焦点

import UIKit

class HJMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //添加TabController的子视图控制器
        addChildViewControllers()
        
        let mainTabbar = HJMainTabBar()
        
        //target - action 是oc中机制
        mainTabbar.plusBtn.addTarget(self, action: #selector(HJMainViewController.plusBtnDidClick), forControlEvents: .TouchUpInside)
       
        
        //赋值, 只读属性不可以直接赋值,但是可以通过 KVC间接赋值
//        self.tabBar = mainTabbar
        //classForCoder 类型字符串的说明
        print(tabBar.classForCoder)
        //setObject :ForKey ==> 字典
        setValue(mainTabbar, forKey: "tabBar")
        
        print(tabBar.classForCoder)
        
    }
    
    //为了让外界不能访问按钮的监听方法,需要添加private私有的方法
    //一旦添加private 这个时候,就不具备动态派发的特性
    //需要添加 @objc 告诉系统这个方法 是基于oc消息动态派发
    
    @objc private func plusBtnDidClick() {
        print("我加号被点击了")
    
    //添加撰写的选择视图
    let composeView = HJComposeView()
        
        composeView.show(targetVC: self)
    
//    let window = UIApplication.sharedApplication().keyWindow
//    window?.addSubview(composeView)
}


    
    //private 表示是私有的意思, 是相对于当前的文件是私有的
    private func addChildViewControllers(){
    
        
        addChildViewController(HJHomeController(), title: "首页", imageName: "tabbar_home",index: 0)
        addChildViewController(HJMessageController(), title: "消息", imageName: "tabbar_message_center",index: 1)
        addChildViewController(HJDiscoverController(), title: "发现", imageName: "tabbar_discover",index: 2)
        addChildViewController(HJProfileController(), title: "我的", imageName: "tabbar_profile",index: 3)

    }
    
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String,index: Int)  {

        
        //如果标签和导航条的title是一样的
        let tabbaritem = HJTabbarItem()
        vc.tabBarItem = tabbaritem

        vc.title = title
        //设置tag
        vc.tabBarItem.tag = index

        vc.tabBarItem.image = UIImage(named: imageName)
        
        //1.设置选中时图片. 需要设置渲染的模式. 2.在xcassets中, render as 原声的
        vc.tabBarItem.selectedImage = UIImage(named:imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //设置title的颜色, NSForegroundColorAttributesName 前景色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName :UIColor.orangeColor()], forState: .Selected)
 
//        //统一设置所有的title的颜色
//        tabBar.tintColor = UIColor.orangeColor()
        
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        
        //内嵌到导航视图控制器中
        let nav = HJBaseNavController(rootViewController: vc)
        //添加子视图控制器
        addChildViewController(nav)
    }
    
    //实现协议方法
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        print(item.tag)
        //遍历'tabBar'子视图
        var index = 0
        for v in tabBar.subviews {
            if v.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                //确定点击的是哪一个tabbarButton
                if index == item.tag{
                    //再遍历子视图 获取背景视图
                    for subView in v.subviews {
                        if subView.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            subView.transform = CGAffineTransformMakeScale(0.5, 0.5)
                            //在动画闭包中 恢复到初始值
                            UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 4, options: [], animations: {
                                subView.transform = CGAffineTransformIdentity
                                }, completion: { (_) in
                                    print("👌")
                            })
                        }
                    }
                }
                index+=1
            }
        }
    }

    

//private 是相对当前文件的
class person: NSObject{
        func haha() {
            let tab = HJMainViewController()
            
            tab.addChildViewControllers()
      }
  }

}

